package model.dba;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import model.IRCursadaSala;
import model.IRHorarioCursada;


public class IRCursadaSalaDAO extends JDBC{
	private static IRCursadaSalaDAO iRCursadaSalaDAO;

	private IRCursadaSalaDAO() {
		super();
	}

	public static IRCursadaSalaDAO getInstance() {
		if (iRCursadaSalaDAO == null) {
			iRCursadaSalaDAO = new IRCursadaSalaDAO();
		}
		return iRCursadaSalaDAO;
	}

	public int add(IRCursadaSala iRCursadaSala) {
		try {

			String queryString = "INSERT INTO iRcursadaSala(idCursadaC,idSalaC)" + " VALUES(?,?) ";
			preparedStatement = connection.prepareStatement(queryString);
			preparedStatement.setLong(1, iRCursadaSala.getidCursada());
			preparedStatement.setLong(2,iRCursadaSala.getidSala());

			preparedStatement.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		return getLastId();

	}

	// En un futuro se puede poner nombre

	public boolean update(IRCursadaSala iRCursadaSala) {
		boolean state = false;
		try {
			String queryString = "UPDATE iRcursadaSala SET idCursadaC=?, idSalaC=? WHERE iRcursadaSala=?";

			preparedStatement = connection.prepareStatement(queryString);

			preparedStatement.setLong(1, iRCursadaSala.getidCursada());
			preparedStatement.setLong(2, iRCursadaSala.getidSala());
			preparedStatement.setLong(3, iRCursadaSala.getidSala());


			state = preparedStatement.executeUpdate() != 0 ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null)
					preparedStatement.close();
			}

			catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();

			}
		}
		return state;
	}

	public boolean delete(Integer idCursadaC) {
		boolean state = false;
		try {
			String queryString = "DELETE FROM iRcursadaSala WHERE idCursadaC=?";

			preparedStatement = connection.prepareStatement(queryString);
			preparedStatement.setInt(1, idCursadaC);
			state = preparedStatement.executeUpdate() != 0 ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return state;

	}

	public IRCursadaSala get(Integer idCursadaC) {

		IRCursadaSala iRCursadaSala = new IRCursadaSala();
		try {
			String queryString = "SELECT * FROM iRcursadaSala where idCursadaC = '" + idCursadaC + "';";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				iRCursadaSala = populate();

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return iRCursadaSala;
	}

	// En un futuro se puede poner nombre

	public IRCursadaSala find(Integer idSalaC) {

		IRCursadaSala iRCursadaSala = new IRCursadaSala();
		try {
			String queryString = "SELECT * FROM iRcursadaSala where idSalaC ='" + idSalaC + "' ;";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {

				iRCursadaSala = populate();

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return iRCursadaSala;
	}

	public int getLastId() {
		int ret = 0;
		try {
			String queryString = "SELECT idHorarioC FROM iRcursadaSala order by idCursadaC DESC LIMIT 1  ";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				ret = resultSet.getInt("idCursadaC");

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		return ret;

	}

	public List<IRCursadaSala> getAll() {
		List<IRCursadaSala> iRCursadaSala = new ArrayList<IRCursadaSala>();

		try {
			String queryString = "SELECT * FROM iRcursadaSala ;";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {

				iRCursadaSala.add(populate());
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return iRCursadaSala;
	}
	
	public List<IRCursadaSala> findAll(Integer idCursadaC) {

		List<IRCursadaSala> areaList = new ArrayList<IRCursadaSala>();
		try {
			String queryString = "SELECT * FROM iRcursadaSala where idCursadaC = '" + idCursadaC + "' ;";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {

				areaList.add(populate());

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return areaList;
	}
	
	public List<IRCursadaSala> findAllXSala(Integer idSala) {

		List<IRCursadaSala> areaList = new ArrayList<IRCursadaSala>();
		try {
			String queryString = "SELECT * FROM iRcursadaSala where idSalaC = '" + idSala + "' ;";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {

				areaList.add(populate());

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return areaList;
	}


	private IRCursadaSala populate() throws SQLException {

		IRCursadaSala iRCursadaSala = new IRCursadaSala();
		iRCursadaSala.setidCursada((int) resultSet.getLong("idCursadaC"));
		iRCursadaSala.setidSala((int) resultSet.getLong("idSalaC"));
		return iRCursadaSala;

	}
}
